import { config } from "../../../config/index"
import Toast from 'tdesign-miniprogram/toast/index';
import i18nInstance from "../../../miniprogram_npm/miniprogram-i18n-plus/index"

Page({
  data: {
    Id: 0
  },

  onLoad(options) {
    i18nInstance.effect(this);
    const { Id } = options;
    this.setData({
      Id: Id
    })
    console.log('Received Id parameter:', Id);
  },
  data: {
    company: '',
    phone: '',
    address: '',
    remark: '',
    contact: ''
  },

  handleInput1(e) {
    this.setData({
      company: e.detail.value
    })
  },

  handleInput2(e) {
    this.setData({
      phone: e.detail.value,
    })
  },

  handleInput3(e) {
    this.setData({
      address: e.detail.value
    })
  },
  handleInput4(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  handleInput5(e) {
    this.setData({
      contact: e.detail.value
    })
  },
  checkPhoneFormat(phoneNumber) {
    const phoneRegex = /^1\d{10}$/;
    return phoneRegex.test(phoneNumber);
  },
  handleSubmit() {
    console.log(this.data.Id);
    if (!this.checkPhoneFormat(this.data.phone)) {
      Toast({
        context: this,
        selector: '#t-toast',
        message: '电话号码不正确，请检查格式',
      });
      return;
    }
    const openid = wx.getStorageSync('openid')
    const now = new Date().toJSON().substring(0, 10) + ' ' + new Date().toTimeString().substring(0, 8);
    console.log(now);
    wx.request({
      url: config.baseUrl + "/AddtoReservation",
      method: "POST",
      header: {
        "content-type": "application/json"
      },
      data: {
        phone_number: this.data.phone,
        userid: openid,
        address: this.data.address,
        remark: this.data.remark,
        Contact: this.data.contact,
        company: this.data.company,
        machine_id: this.data.Id,
        time: now
      },
      success(e) {
        console.log(e);
        Toast({
          context: this,
          selector: '#t-toast',
          message: '预定成功',
        });
      },
      fail(e) {
        console.log(e);
        Toast({
          context: this,
          selector: '#t-toast',
          message: '网络有点问题，稍后再试',
        });
      }
    })
  }
})